#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        map<int, int> M;
        vector<int> T;
        for (int i = 0; i < arr1.size(); i++) {
            M[arr1[i]] = M[arr1[i]] + 1;
        }

        for (int i = 0; i < arr2.size(); i++) {
            for (int j = 0; j < M[arr2[i]]; j++) {
                T.push_back(arr2[i]);
            }
            M.erase(arr2[i]);
        }

        for (map<int, int>::iterator i = M.begin(); i != M.end(); i++) {
            for (int j = 0; j < i->second; j++) {
                T.push_back(i->first);
            }
        }
        return T;
        

    }
};


//int main() {
//    vector<int> arr1 = { 2,3,1,3,2,4,6,7,9,2,19 };
//    vector<int> arr2 = { 2,1,4,3,9,6 };
//    Solution S;
//    vector<int> T = S.relativeSortArray(arr1, arr2);
//    return 0;
//
//}